﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;

namespace TomAndJerryMidnightSnack
{

    public class PlayScreen : GameScreen
    {
        public enum MODE { NONE, RESET, MOVEITEM, MOVE };
        public enum TYPEDRAG { UP, DOWN, LEFT, RIGHT, LOXO};

        SmokeSprite Khoi;
        SpriteBatch SpriteBatch;
        Texture2D BackgroundTexture;
        Texture2D PanelScoreTexture;

        Texture2D KhayTexture;
        Texture2D LoXoBigTexture;

        Texture2D HoSauTexture;
        Texture2D WindTexture;
        Texture2D XoayTexture;

        Texture2D PhoMatTexture;
        Texture2D KeyTexture;

        Texture2D ArrowLeftOranTexture;
        Texture2D ArrowRightOranTexture;
        Texture2D ArrowUpOranTexture;
        Texture2D ArrowDownOranTexture;

        Texture2D ArrowLeftTexture;
        Texture2D ArrowRightTexture;
        Texture2D ArrowUpTexture;
        Texture2D ArrowDownTexture;
        Texture2D LoXoSmallTexture;

        Controller controller;
        DoorSprite DoorStart;
        DoorSprite DoorStop;
        JerrySprite Jerry;

        LifeSprite[] Lifes;
        int ExistLife;

        SpriteFont font;

        public int ExistUp, ExistDown, ExistLeft, ExistRight, ExistLoXo;
        LevelTask Task;
        public int CountLife { set; get; }
        Vector2 KhayPosition;
        Vector2 LoXoBigPosition;

        Vector2 CurrentOval;
        double Alpha;
        float LengthStep;
        int eslapTime;

        int Mode;

        int TypeArrowCurrent;
        public Vector2 CurrentArrow;
        Texture2D CurrentTexturwArrow;

        List<Vector2> ListArrowLeft;
        List<Vector2> ListArrowRight;
        List<Vector2> ListArrowUp;
        List<Vector2> ListArrowDown;
        List<Vector2> ListLoXo;

        List<Vector2> ListPhoMat;

        List<WindSprite> ListWind;
        List<Vector2> KeyLocation;
        int Score;

        /// <summary>
        /// Constructor.
        /// </summary>
        public PlayScreen()
            : base()
        {
            //sound effect for game
            //AudioManager.PushMusic("LoseTheme",false);
            this.Exiting += new EventHandler(PlayScreen_Exiting);
            controller = new Controller();
            DoorStop = new DoorSprite();
            DoorStart = new DoorSprite();
            Jerry = new JerrySprite();
            Khoi = new SmokeSprite();

            Lifes = new LifeSprite[5];
            ExistLife = 5;
            CountLife = 5;

            switch (ScreenManager.Level)
            {
                case 1:
                    Task = new LevelTask1();
                    break;
                case 2:
                    Task = new LevelTask2();
                    break;
                case 3:
                    Task = new LevelTask3();
                    break;
                case 4:
                    Task = new LevelTask4();
                    break;
                case 5:
                    Task = new LevelTask5();
                    break;
                case 6:
                    Task = new LevelTask6();
                    break;
                case 7:
                    Task = new LevelTask7();
                    break;
                case 8:
                    Task = new LevelTask8();
                    break;
                case 9:
                    Task = new LevelTask9();
                    break;
                case 10:
                    Task = new LevelTask10();
                    break;
                default:
                    break;
            }
            ExistUp = Task.CountUp;
            ExistDown = Task.CountDown;
            ExistLeft = Task.CountLeft;
            ExistRight = Task.CountRight;
            ExistLoXo = Task.CountLoXo;
            CurrentOval = Task.StartLocation;
            Mode = (int)MODE.NONE;
            eslapTime = 0;

            ListArrowDown = new List<Vector2>();
            ListArrowLeft = new List<Vector2>();
            ListArrowRight = new List<Vector2>();
            ListArrowUp = new List<Vector2>();
            ListLoXo = new List<Vector2>();

            ListPhoMat = new List<Vector2>();

            CurrentTexturwArrow = null;
            CurrentArrow = Vector2.Zero;

            ListPhoMat = Task.PhomatList;
            KeyLocation = Task.KeyList;

            ListWind = new List<WindSprite>();
            for (int i = 0; i < Task.WindList.Count; i++)
            {
                ListWind.Add(new WindSprite());
            }
        }

        public void Reset()
        {
            Score = 0;
            switch (ScreenManager.Level)
            {
                case 1:
                    Task = new LevelTask1();
                    break;
                case 2:
                    Task = new LevelTask2();
                    break;
                case 3:
                    Task = new LevelTask3();
                    break;
                case 4:
                    Task = new LevelTask4();
                    break;
                case 5:
                    Task = new LevelTask5();
                    break;
                case 6:
                    Task = new LevelTask6();
                    break;
                case 7:
                    Task = new LevelTask7();
                    break;
                case 8:
                    Task = new LevelTask8();
                    break;
                case 9:
                    Task = new LevelTask9();
                    break;
                case 10:
                    Task = new LevelTask10();
                    break;
                default:
                    break;
            }

            ListArrowDown.Clear();
            ListArrowDown = new List<Vector2>();
            ListArrowLeft.Clear();
            ListArrowLeft = new List<Vector2>();
            ListArrowUp.Clear();
            ListArrowUp = new List<Vector2>();
            ListArrowRight.Clear();
            ListArrowRight = new List<Vector2>();
            ListLoXo.Clear();
            ListLoXo = new List<Vector2>();

            Mode = (int)MODE.NONE;
            if (ExistLife == 0)
            {
                GameScreen Lose = new LoseScreen();
                ScreenManager.Score = 0;
                ScreenManager.Level = 1;
                ExitScreen();
                ScreenManager.AddScreen(Lose);
                return;
            }
            Lifes[CountLife - ExistLife--].LifeAnimation.currentFrame = 1;

            Jerry.Direction = Task.Direct;
            Jerry.Position = new Vector2(MatrixLocation.getInstance().mapLocations[(int)Task.StartLocation.X][(int)Task.StartLocation.Y].X - 10, MatrixLocation.getInstance().mapLocations[(int)Task.StartLocation.X][(int)Task.StartLocation.Y].Y + 610);

            ExistUp = Task.CountUp;
            ExistDown = Task.CountDown;
            ExistLeft = Task.CountLeft;
            ExistRight = Task.CountRight;
            ExistLoXo = Task.CountLoXo;
            CurrentOval = Task.StartLocation;
            Mode = (int)MODE.NONE;
            eslapTime = 0;

            CurrentTexturwArrow = null;
            CurrentArrow = Vector2.Zero;

            ListPhoMat = Task.PhomatList;

            KeyLocation = Task.KeyList;

            Jerry.Direction = Task.Direct;
            Jerry.JerryAnimation.CurrentRow = Task.Direct;
            Jerry.JerryAnimation.CurrentFrame = 0;
            DoorStop.DoorAnimation.currentFrame = Task.CurrentFrame;
        }

        void PlayScreen_Exiting(object sender, EventArgs e)
        {
            //stop music
          //  MediaPlayer.Stop();
        }

        public override void LoadContent()
        {
            SpriteBatch = ScreenManager.SpriteBatch;
            ContentManager content = ScreenManager.Game.Content;
            Viewport viewport = ScreenManager.GraphicsDevice.Viewport;

            AnimationState animationController = new AnimationState();
            Vector2 controlerPosition = new Vector2(-40, 541 + 777);
            Texture2D controlerTexture = content.Load<Texture2D>(@"MainItems\ControlerAmination");
            animationController.Initialize(controlerTexture, controlerPosition, 259, 180, 3, 30, Color.White, 1f, false);
            controller.Initialize(animationController, controlerPosition);

            Vector2 DoorStartPosition;
            Vector2 DoorStopPosition;
            DoorStartPosition = Task.DoorStartLocation;
            DoorStopPosition = Task.DoorStopLocation;
            
            Texture2D DoorLeftTexture = content.Load<Texture2D>(@"MainItems\doorleft");
            Texture2D DoorRightTexture = content.Load<Texture2D>(@"MainItems\doorright");

            AnimationState animationDoorStart = new AnimationState();
            if (DoorStartPosition.Y < 550)
            {
                animationDoorStart.Initialize(DoorLeftTexture, DoorStartPosition, 110, 119, 2, 30, Color.White, 1f, false);
            }
            else
            {
                animationDoorStart.Initialize(DoorRightTexture, DoorStartPosition, 110, 119, 2, 30, Color.White, 1f, false);
            }
            DoorStart.Initialize(animationDoorStart, DoorStartPosition);
            DoorStart.DoorAnimation.currentFrame = 1;

            AnimationState animationDoorStop = new AnimationState();
            if (DoorStopPosition.Y < 550)
            {
                animationDoorStop.Initialize(DoorLeftTexture, DoorStopPosition, 110, 119, 2, 30, Color.White, 1f, false);
            }
            else
            {
                animationDoorStop.Initialize(DoorRightTexture, DoorStopPosition, 110, 119, 2, 30, Color.White, 1f, false);
            }
            DoorStop.Initialize(animationDoorStop, DoorStopPosition);
            DoorStop.DoorAnimation.currentFrame = Task.CurrentFrame;

            Texture2D LifeTexture = content.Load<Texture2D>(@"MainItems\life");
            Vector2[] LifePos = { new Vector2(420, 790), new Vector2(420, 830), new Vector2(420, 870), new Vector2(380, 810), new Vector2(380, 850) };
            for (int i = 0; i < LifePos.Length; i++)
            {
                AnimationState animationLife = new AnimationState();
                LifeSprite sprite = new LifeSprite();
                animationLife.Initialize(LifeTexture, LifePos[i], 60, 50, 2, 30, Color.White, 1f, false);
                sprite.Initialize(animationLife, LifePos[i]);
                Lifes[i] = sprite;
            }

            BackgroundTexture = content.Load<Texture2D>(@"bground");
            PanelScoreTexture = content.Load<Texture2D>(@"MainItems\PanelScore");

            KhayTexture = content.Load<Texture2D>(@"MainItems\KhayOk");
            LoXoBigTexture = content.Load<Texture2D>(@"MainItems\LoXoBig");

            HoSauTexture = content.Load<Texture2D>(@"MainItems\hosau");
            XoayTexture = content.Load<Texture2D>(@"MainItems\Xoay");
            WindTexture = content.Load<Texture2D>(@"MainItems\Wind");

            for (int i = 0; i < Task.WindList.Count; i++)
            {
                Vector2 WindPosition = new Vector2(MatrixLocation.getInstance().mapLocations[(int)Task.WindList[i].X][(int)Task.WindList[i].Y].X - 25, MatrixLocation.getInstance().mapLocations[(int)Task.WindList[i].X][(int)Task.WindList[i].Y].Y + 1300);
                Animation animationWind = new Animation();
                animationWind.Initialize(WindTexture, WindPosition, 90, 75, 15, 50, Color.White, 1f, true);
                ListWind[i].Initialize(animationWind, WindPosition);
            }


            font = content.Load<SpriteFont>(@"Fonts\gameFont");

            ArrowLeftOranTexture = content.Load<Texture2D>(@"Oran\LeftOran");
            ArrowRightOranTexture = content.Load<Texture2D>(@"Oran\RightOran");
            ArrowUpOranTexture = content.Load<Texture2D>(@"Oran\UpOran");
            ArrowDownOranTexture = content.Load<Texture2D>(@"Oran\DownOran");

            ArrowLeftTexture = content.Load<Texture2D>(@"MainItems\ArrowLeft");
            ArrowRightTexture = content.Load<Texture2D>(@"MainItems\ArrowRight");
            ArrowUpTexture = content.Load<Texture2D>(@"MainItems\ArrowUp");
            ArrowDownTexture = content.Load<Texture2D>(@"MainItems\ArrowDown");
            LoXoSmallTexture = content.Load<Texture2D>(@"MainItems\LoXoSmall");

            PhoMatTexture = content.Load<Texture2D>(@"MainItems\phomat");
            KeyTexture = content.Load<Texture2D>(@"MainItems\Key");

            AnimationJerry animationJerry = new AnimationJerry();
            Texture2D JerryTexture = content.Load<Texture2D>(@"Jerry");
            Vector2 JerryPosition = new Vector2(MatrixLocation.getInstance().mapLocations[(int)Task.StartLocation.X][(int)Task.StartLocation.Y].X - 10, MatrixLocation.getInstance().mapLocations[(int)Task.StartLocation.X][(int)Task.StartLocation.Y].Y + 610);
            animationJerry.Initialize(JerryTexture, JerryPosition, 70, 105, 9, 250, Color.White, 1f, true);
            Jerry.Initialize(animationJerry, JerryPosition);
            Jerry.Direction = Task.Direct;            
            Jerry.JerryAnimation.CurrentRow = Task.Direct;
            Jerry.JerryAnimation.CurrentFrame = 0;

            Animation animationKhoi = new Animation();
            Texture2D KhoiTexture = content.Load<Texture2D>(@"MainItems\Smoke");
            Vector2 KhoiPosition = Vector2.Zero;
            animationKhoi.Initialize(KhoiTexture, KhoiPosition, 100, 73, 4, 250, Color.White, 1f, false);
            Khoi.Initialize(animationKhoi, KhoiPosition);

            Mode = (int) MODE.NONE;
            KhayPosition = new Vector2(130, -35);
            LoXoBigPosition = new Vector2(90,170);
            try
            {
              //  backgroundMusic = content.Load<Song>("Sounds\\sound023_gameloop");
               // MediaPlayer.Play(backgroundMusic);
                //MediaPlayer.IsRepeating = true;
            }
            catch { }
                
        }

        /// <summary>
        /// UnloadContent
        /// </summary>

        public override void UnloadContent()
        {
            base.UnloadContent();
        }

        /// <summary>
        /// Handles user input.
        /// </summary>
        public override void HandleInput()
        {
            if (Mode == (int)MODE.NONE)
            {
                if (InputManager.IsButtonClicked(new Rectangle( (int)120 - 80, (int)605,(int)80,(int)60)) )
                {
                    controller.ControllerAnimation.currentFrame = 1;
                    Mode = (int)MODE.MOVE;
                }
                else if (InputManager.IsButtonClicked(new Rectangle( (int)120 - 70, (int)700, (int)80,(int)60)) )
                {
                    controller.ControllerAnimation.currentFrame = 2;
                    Reset();
                }
                else
                {
                    if ((ExistUp - ListArrowUp.Count) >0 && InputManager.IsButtonClicked(new Rectangle(80, 5, (int)46, (int)65)))//up
                    {
                        Mode = (int)MODE.MOVEITEM;
                        TypeArrowCurrent = (int)TYPEDRAG.UP;
                        CurrentArrow = new Vector2();
                        CurrentTexturwArrow = ArrowUpTexture;
                    }
                    else if (((ExistDown - ListArrowDown.Count) >0) && InputManager.IsButtonClicked(new Rectangle(20, (int)103, (int)42, (int)62)))//down
                    {
                        Mode = (int)MODE.MOVEITEM;
                        CurrentArrow = new Vector2();
                        CurrentTexturwArrow = ArrowDownTexture;
                        TypeArrowCurrent = (int)TYPEDRAG.DOWN;
                    }
                    else if (((ExistLeft - ListArrowLeft.Count) > 0)  && InputManager.IsButtonClicked(new Rectangle(20, (int)2, (int)46, (int)65)))//left
                    {
                        Mode = (int)MODE.MOVEITEM;
                        CurrentArrow = new Vector2();
                        CurrentTexturwArrow = ArrowLeftTexture;
                        TypeArrowCurrent = (int)TYPEDRAG.LEFT;
                    }
                    else if (((ExistRight - ListArrowRight.Count) > 0) && InputManager.IsButtonClicked(new Rectangle(80, (int)103, (int)46, (int)69)))//right
                    {
                        Mode = (int)MODE.MOVEITEM;
                        CurrentArrow = new Vector2();
                        CurrentTexturwArrow = ArrowRightTexture;
                        TypeArrowCurrent = (int)TYPEDRAG.RIGHT;
                    }
                    else if (((ExistLoXo - ListLoXo.Count) > 0) && InputManager.IsButtonClicked(new Rectangle(30, (int)200, (int)68, (int)60)))
                    {
                        Mode = (int)MODE.MOVEITEM;
                        CurrentArrow = new Vector2();
                        CurrentTexturwArrow = LoXoSmallTexture;
                        TypeArrowCurrent = (int)TYPEDRAG.LOXO;
                    }
                    else
                    {
                        bool isDetected = false;
                        //Debug.WriteLine("troi oi");
                        foreach (Vector2 vector in ListArrowDown)
                        {
                            Vector2 v = MatrixLocation.getInstance().mapLocations[(int)vector.X][(int)vector.Y];
                            //Debug.WriteLine(v.X + "--"+ v.Y);
                            if (InputManager.IsButtonClicked(new Rectangle((int)v.X-50, (int)v.Y-70, (int)50, (int)70)))//right
                            {
                                Mode = (int)MODE.MOVEITEM;
                                CurrentArrow = new Vector2();
                                CurrentTexturwArrow = ArrowDownTexture;
                                TypeArrowCurrent = (int)TYPEDRAG.DOWN;
                                isDetected = true;
                                ListArrowDown.Remove(vector);
                                break;
                            }
                        }
                        if (!isDetected)
                        {
                            foreach (Vector2 vector in ListArrowUp)
                            {
                                Vector2 v = MatrixLocation.getInstance().mapLocations[(int)vector.X][(int)vector.Y];
                                if (InputManager.IsButtonClicked(new Rectangle((int)v.X-50, (int)v.Y-70, (int)50, (int)70)))//right
                                {
                                    Mode = (int)MODE.MOVEITEM;
                                    CurrentArrow = new Vector2();
                                    CurrentTexturwArrow = ArrowUpTexture;
                                    TypeArrowCurrent = (int)TYPEDRAG.UP;
                                    isDetected = true;
                                    ListArrowUp.Remove(vector);
                                    break;
                                }
                            }
                            if (!isDetected)
                            {
                                foreach (Vector2 vector in ListArrowLeft)
                                {
                                    Vector2 v = MatrixLocation.getInstance().mapLocations[(int)vector.X][(int)vector.Y];
                                    if (InputManager.IsButtonClicked(new Rectangle((int)v.X-50, (int)v.Y-70, (int)50, (int)70)))//right
                                    {
                                        Mode = (int)MODE.MOVEITEM;
                                        CurrentArrow = new Vector2();
                                        CurrentTexturwArrow = ArrowLeftTexture;
                                        TypeArrowCurrent = (int)TYPEDRAG.LEFT;
                                        isDetected = true;
                                        ListArrowLeft.Remove(vector);
                                        break;
                                    }
                                }
                                if (!isDetected)
                                {
                                    foreach (Vector2 vector in ListArrowRight)
                                    {
                                        Vector2 v = MatrixLocation.getInstance().mapLocations[(int)vector.X][(int)vector.Y];
                                        if (InputManager.IsButtonClicked(new Rectangle((int)v.X-50, (int)v.Y-70, (int)50, (int)70)))//right
                                        {
                                            Mode = (int)MODE.MOVEITEM;
                                            CurrentArrow = new Vector2();
                                            CurrentTexturwArrow = ArrowRightTexture;
                                            TypeArrowCurrent = (int)TYPEDRAG.RIGHT;
                                            ListArrowRight.Remove(vector);
                                            isDetected = true;
                                            break;
                                        }
                                    }

                                    if (!isDetected)
                                    {
                                        foreach (Vector2 vector in ListLoXo)
                                        {
                                            Vector2 v = MatrixLocation.getInstance().mapLocations[(int)vector.X][(int)vector.Y];
                                            if (InputManager.IsButtonClicked(new Rectangle((int)v.X-68, (int)v.Y-60, (int)68, (int)60)))//right
                                            {
                                                Mode = (int)MODE.MOVEITEM;
                                                CurrentArrow = new Vector2();
                                                CurrentTexturwArrow = LoXoSmallTexture;
                                                TypeArrowCurrent = (int)TYPEDRAG.LOXO;
                                                ListLoXo.Remove(vector);
                                                isDetected = true;
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    controller.ControllerAnimation.currentFrame = 0;
                }
            }
            else if (Mode == (int)MODE.MOVEITEM)
            {
                if (!InputManager.DragArrow(this))
                {
                    Mode = (int)MODE.NONE;
                    Vector2 v = FindNearest(new Vector2(CurrentArrow.X, CurrentArrow.Y));
                    if (v == Vector2.Zero)
                    {
                        CurrentArrow = Vector2.Zero;
                        CurrentTexturwArrow = null;
                        return;
                    }
                    CurrentArrow = MatrixLocation.getInstance().mapLocations[(int)v.X][(int)v.Y];
                    //CurrentArrow.X += 22;
                    //CurrentArrow.Y -= 55;
                    switch (TypeArrowCurrent)
                    {
                        case 0:
                            ListArrowUp.Add(v);
                          //  ExistUp--;
                            break;
                        case 1:
                            ListArrowDown.Add(v);
                           // ExistDown--;
                            break;
                        case 2:
                            ListArrowLeft.Add(v);
                           // ExistLeft--;
                            break;
                        case 3:
                            ListArrowRight.Add(v);
                          //  ExistRight--;
                            break;
                        case 4:
                            ListLoXo.Add(v);
                           // ExistLoXo--;
                            break;
                        default:
                            break;
                    }
                    CurrentArrow = Vector2.Zero;
                    CurrentTexturwArrow = null;
                }
            }
            else if (Mode == (int)MODE.MOVE)
            {
                if (InputManager.IsButtonClicked(new Rectangle((int)120 - 70, (int)700, (int)80, (int)60)) )
                {
                    controller.ControllerAnimation.currentFrame = 2;
                    Reset();
                }
                else
                {
                    controller.ControllerAnimation.currentFrame = 0;
                }
            }
            base.HandleInput();
        }


        public Vector2 FindNearest(Vector2 oval)
        {
            if (oval.X < 19 || oval.X > 331 || oval.Y < 4 || oval.Y > 767)
            {
                return Vector2.Zero;
            }
            {
                float nX = oval.X / 22-1;
                float nY = oval.Y / 55 -1;
                int x1 = (int)Math.Round(nX);
                int x2 = (int)Math.Floor(nX);
                int y1 = (int)Math.Round(nY);
                int y2 = (int)Math.Floor(nY);
                if (x1 <= 0) x1 = 0;
                if (y1 <= 0) y1 = 0;
                if (x2 <= 0) x2 = 0;
                if (y2 <= 0) y2 = 0;
                if (x1 >= 13) x1 = 12;
                if (x2 >= 13) x2 = 12;
                if (y1 >= 13) y1 = 12;
                if (y2 >= 13) y2 = 12;
                double min = 1000f;
                Vector2 ok = Vector2.Zero;
                Vector2 v1 = MatrixLocation.getInstance().mapLocations[y1][x1];
                Vector2 v2 = MatrixLocation.getInstance().mapLocations[y1][x2];
                Vector2 v3 = MatrixLocation.getInstance().mapLocations[y2][x2];
                Vector2 v4 = MatrixLocation.getInstance().mapLocations[y2][x1];

                if (v1 != Vector2.Zero)
                {
                    double size = Math.Sqrt(Math.Pow(oval.X - v1.X, 2) + Math.Pow(oval.Y - v1.Y, 2));
                    if (size < min)
                    {
                        min = size;
                        ok = new Vector2(y1, x1);
                    }
                }

                if (v2 != Vector2.Zero)
                {
                    double size = Math.Sqrt(Math.Pow(v2.X - oval.X, 2) + Math.Pow(oval.Y - v2.Y, 2));
                    if (size < min)
                    {
                        min = size;
                        ok = new Vector2(y1, x2);
                    }
                }

                if (v3 != Vector2.Zero)
                {
                    double size = Math.Sqrt(Math.Pow(v3.X - oval.X, 2) + Math.Pow(v3.Y - oval.Y, 2));
                    if (size < min)
                    {
                        min = size;
                        ok = new Vector2(y2, x2);
                    }
                }

                if (v4 != Vector2.Zero)
                {
                    double size = Math.Sqrt(Math.Pow(oval.X - v4.X, 2) + Math.Pow(v4.Y - oval.Y, 2));
                    if (size < min)
                    {
                        min = size;
                        ok = new Vector2(y2, x1);
                    }
                }

                if (ok != Vector2.Zero)
                {
                    if (ListPhoMat.Contains(ok)) ok = Vector2.Zero;
                    else if (ListLoXo.Contains(ok)) ok = Vector2.Zero;
                    else if (ListArrowDown.Contains(ok)) ok = Vector2.Zero;
                    else if (ListArrowLeft.Contains(ok)) ok = Vector2.Zero;
                    else if (ListArrowRight.Contains(ok)) ok = Vector2.Zero;
                    else if (ListArrowUp.Contains(ok)) ok = Vector2.Zero;
                    else if (KeyLocation.Contains(ok)) ok = Vector2.Zero;
                    else if (Task.StopLocation == ok) ok = Vector2.Zero;
                    else if (Task.XoayList1.Contains(ok)) ok = Vector2.Zero;
                    else if (Task.XoayList2.Contains(ok)) ok = Vector2.Zero;
                    else if (Task.WindList.Contains(ok)) ok = Vector2.Zero;
                    else if (Task.HoSauList.Contains(ok)) ok = Vector2.Zero;
                    else if (Task.RightListReady.Contains(ok)) ok = Vector2.Zero;
                    else if (Task.LeftListReady.Contains(ok)) ok = Vector2.Zero;
                    else if (Task.UpListReady.Contains(ok)) ok = Vector2.Zero;
                    else if (Task.DownListReady.Contains(ok)) ok = Vector2.Zero;
                    else if (Task.StartLocation==ok) ok = Vector2.Zero;
                }

                return ok;
            }

        }

        /// <summary>
        /// Updates the menu.
        /// </summary>
        public override void Update(GameTime gameTime, bool otherScreenHasFocus,
                                                       bool coveredByOtherScreen)
        {
            if (!ScreenManager.IsPause)
            {
                controller.Update(gameTime);
                DoorStop.Update(gameTime);
                DoorStart.Update(gameTime);
                for (int i = 0; i < Lifes.Length; i++)
                {
                    Lifes[i].Update(gameTime);
                }
                if (Jerry.Direction == (int)MatrixLocation.DIRECTIONS.FALL)
                {
                    Khoi.Update(gameTime);
                }
                else
                {
                    UpdateJerry(gameTime);
                }

                foreach (WindSprite win in ListWind)
                {
                    win.Update(gameTime);
                }
                if (Jerry.Direction == (int)MatrixLocation.DIRECTIONS.FALL && !Khoi.KhoiAnimation.Active)
                {
                    Reset();
                }
                SpriteBatch.End();
                base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
            }
        }

        public void UpdateJerry(GameTime gameTime)
        {
            eslapTime += gameTime.ElapsedGameTime.Milliseconds;
            if (Mode == (int)MODE.MOVE && eslapTime > 250)
            {
                if (Jerry.Direction == (int)MatrixLocation.DIRECTIONS.DOWN)
                {
                    if (Jerry.Step == 0)
                    {
                        double deltaX = MatrixLocation.getInstance().mapLocations[(int)CurrentOval.X][(int)CurrentOval.Y].X - MatrixLocation.getInstance().mapLocations[(int)CurrentOval.X + 1][(int)CurrentOval.Y - 1].X;
                        double deltaY = MatrixLocation.getInstance().mapLocations[(int)CurrentOval.X + 1][(int)CurrentOval.Y - 1].Y - MatrixLocation.getInstance().mapLocations[(int)CurrentOval.X][(int)CurrentOval.Y].Y;
                        LengthStep = (float)deltaY / 4;
                        Alpha = deltaX / deltaY;
                    }
                    float stepLength = (float)Alpha * LengthStep;
                    Jerry.Position.X -= stepLength;
                    Jerry.Position.Y += LengthStep;
                    Jerry.Step++;

                    if (Jerry.Step >= 4)
                    {
                        //dang o tai o nay||
                        CurrentOval.X++;
                        CurrentOval.Y--;
                        Jerry.Step = 0;

                        if (Task.StopLocation == CurrentOval && DoorStop.DoorAnimation.currentFrame == 1 && Jerry.Direction == Task.DirectWin)
                        {
                            GameScreen screen = null;
                            ScreenManager.Level++;
                            if (ScreenManager.Level > ScreenManager.MaxLevel)
                            {
                                screen = new HomeScreen();
                                ScreenManager.Score = 0 ;
                                ScreenManager.Level = 1;
                            }
                            else
                            {
                                screen = new ScoreScreen();
                                ScreenManager.Score += Score;
                            }
                            ExitScreen();
                            ScreenManager.AddScreen(screen);
                            //ScreenManager.RemoveScreen(this);
                        }
                        else if (ListArrowUp.Contains(CurrentOval))
                        {
                            Jerry.Direction = (int)MatrixLocation.DIRECTIONS.UP;
                            Jerry.JerryAnimation.CurrentRow = 1;
                            Jerry.JerryAnimation.CurrentFrame = 0;
                            Jerry.Step = 0;
                            Score += 100;
                            return;
                        }
                        else if (Task.UpListReady.Contains(CurrentOval))
                        {
                            Jerry.Direction = (int)MatrixLocation.DIRECTIONS.UP;
                            Jerry.JerryAnimation.CurrentRow = 1;
                            Jerry.JerryAnimation.CurrentFrame = 0;
                            Jerry.Step = 0;
                            Score += 100;
                            Task.UpListReady.Remove(CurrentOval);
                            return;
                        }
                        else if (ListArrowLeft.Contains(CurrentOval))
                        {
                            Jerry.Direction = (int)MatrixLocation.DIRECTIONS.LEFT;
                            Jerry.Step = 0;
                            Jerry.JerryAnimation.CurrentRow = 2;
                            Jerry.JerryAnimation.CurrentFrame = 0;
                            Score += 100;
                            return;
                        }
                        else if (Task.LeftListReady.Contains(CurrentOval))
                        {
                            Jerry.Direction = (int)MatrixLocation.DIRECTIONS.LEFT;
                            Jerry.Step = 0;
                            Jerry.JerryAnimation.CurrentRow = 2;
                            Jerry.JerryAnimation.CurrentFrame = 0;
                            Score += 100;
                            Task.LeftListReady.Remove(CurrentOval);
                            return;
                        }
                        else if (ListArrowRight.Contains(CurrentOval))
                        {
                            Jerry.Direction = (int)MatrixLocation.DIRECTIONS.RIGHT;
                            Jerry.Step = 0;
                            Jerry.JerryAnimation.CurrentRow = 3;
                            Jerry.JerryAnimation.CurrentFrame = 0;
                            Score += 100;
                            return;
                        }
                        else if (Task.RightListReady.Contains(CurrentOval))
                        {
                            Jerry.Direction = (int)MatrixLocation.DIRECTIONS.RIGHT;
                            Jerry.Step = 0;
                            Jerry.JerryAnimation.CurrentRow = 3;
                            Jerry.JerryAnimation.CurrentFrame = 0;
                            Score += 100;
                            Task.RightListReady.Remove(CurrentOval);
                            return;
                        }
                        else if (Task.DownListReady.Contains(CurrentOval))
                        {
                            Task.DownListReady.Remove(CurrentOval);
                        }
                        else if (ListLoXo.Contains(CurrentOval))
                        {
                            Jerry.JerryAnimation.Jump = true;
                            Score += 200;
                        }
                        else if (Task.WindList.Contains(CurrentOval))
                        {
                            if (!Jerry.JerryAnimation.Jump)
                            {
                                Jerry.Direction = (int)MatrixLocation.DIRECTIONS.FLY;
                            }
                        }
                        else if (Task.HoSauList.Contains(CurrentOval))
                        {
                            if (!Jerry.JerryAnimation.Jump)
                            {
                                Jerry.Direction = (int)MatrixLocation.DIRECTIONS.FALL;
                                Mode = (int)MODE.NONE;
                                Vector2 vt = MatrixLocation.getInstance().mapLocations[(int)CurrentOval.X][(int)CurrentOval.Y];
                                Khoi.Position = vt;
                                Khoi.Position.Y += 350;
                                Khoi.Position.X -= 20;
                                Khoi.Active = true;
                            }
                        }
                        else if (Task.XoayList1.Contains(CurrentOval))
                        {
                            if (Task.XoayList1[0] == CurrentOval)
                            {
                                CurrentOval = Task.XoayList1[1];
                            }
                            else
                            {
                                CurrentOval = Task.XoayList1[0];
                            }
                            Jerry.Position = MatrixLocation.getInstance().mapLocations[(int)CurrentOval.X][(int)CurrentOval.Y];
                            Jerry.Position.X -= 10;
                            Jerry.Position.Y += 610;
                        }
                        else if (ListPhoMat.Contains(CurrentOval))
                        {
                            ListPhoMat.Remove(CurrentOval);
                            Score += 1000;
                        }
                        else if (KeyLocation.Contains(CurrentOval))
                        {
                            KeyLocation.Remove(CurrentOval);
                            DoorStop.DoorAnimation.currentFrame = 1;
                            Score += 200;
                        }
                    }

                    if (CurrentOval == new Vector2(6, 0) ||
                        CurrentOval == new Vector2(7, 1) ||
                        CurrentOval == new Vector2(8, 2) ||
                        CurrentOval == new Vector2(9, 3) ||
                        CurrentOval == new Vector2(10, 4) ||
                        CurrentOval == new Vector2(11, 5) ||
                        CurrentOval == new Vector2(12, 6))
                    {
                        Jerry.Direction = (int)MatrixLocation.DIRECTIONS.UP;
                        Jerry.Step = 0;
                        Jerry.JerryAnimation.CurrentRow = 1;
                        Jerry.JerryAnimation.CurrentFrame = 0;
                    }
                }
                else if (Jerry.Direction == (int)MatrixLocation.DIRECTIONS.UP)
                {
                    if (Jerry.Step == 0)
                    {
                        double deltaX = MatrixLocation.getInstance().mapLocations[(int)CurrentOval.X - 1][(int)CurrentOval.Y + 1].X - MatrixLocation.getInstance().mapLocations[(int)CurrentOval.X][(int)CurrentOval.Y].X;
                        double deltaY = MatrixLocation.getInstance().mapLocations[(int)CurrentOval.X][(int)CurrentOval.Y].Y - MatrixLocation.getInstance().mapLocations[(int)CurrentOval.X - 1][(int)CurrentOval.Y + 1].Y;
                        LengthStep = (float)deltaY / 4;
                        Alpha = deltaX / deltaY;
                    }
                    float stepLength = (float)Alpha * LengthStep;
                    Jerry.Position.X += stepLength;
                    Jerry.Position.Y -= LengthStep;
                    Jerry.Step++;
                    if (Jerry.Step >= 4)
                    {
                        CurrentOval.X--;
                        CurrentOval.Y++;
                        Jerry.Step = 0;

                        if (Task.StopLocation == CurrentOval && DoorStop.DoorAnimation.currentFrame == 1 && Jerry.Direction == Task.DirectWin)
                        {
                            GameScreen screen = null;
                            ScreenManager.Level++;
                            if (ScreenManager.Level > ScreenManager.MaxLevel)
                            {
                                screen = new HomeScreen();
                                ScreenManager.Score = 0;
                                ScreenManager.Level = 1;
                            }
                            else
                            {
                                screen = new ScoreScreen();
                                ScreenManager.Score += Score;
                            }
                            ExitScreen();
                            ScreenManager.AddScreen(screen);
                            //ScreenManager.RemoveScreen(this);
                        }
                        else if (Task.UpListReady.Contains(CurrentOval))
                        {
                            Task.UpListReady.Remove(CurrentOval);
                        }
                        else if (ListArrowDown.Contains(CurrentOval))
                        {
                            Jerry.Direction = (int)MatrixLocation.DIRECTIONS.DOWN;
                            Jerry.Step = 0;
                            Jerry.JerryAnimation.CurrentRow = 0;
                            Jerry.JerryAnimation.CurrentFrame = 0;
                            Score += 100;
                            return;
                        }
                        else if (Task.DownListReady.Contains(CurrentOval))
                        {
                            Jerry.Direction = (int)MatrixLocation.DIRECTIONS.DOWN;
                            Jerry.Step = 0;
                            Jerry.JerryAnimation.CurrentRow = 0;
                            Jerry.JerryAnimation.CurrentFrame = 0;
                            Score += 100;
                            Task.DownListReady.Remove(CurrentOval);
                            return;
                        }
                        else if (ListArrowLeft.Contains(CurrentOval))
                        {
                            Jerry.Direction = (int)MatrixLocation.DIRECTIONS.LEFT;
                            Jerry.Step = 0;
                            Jerry.JerryAnimation.CurrentRow = 2;
                            Jerry.JerryAnimation.CurrentFrame = 0;
                            Score += 100;
                            return;
                        }
                        else if (Task.LeftListReady.Contains(CurrentOval))
                        {
                            Jerry.Direction = (int)MatrixLocation.DIRECTIONS.LEFT;
                            Jerry.Step = 0;
                            Jerry.JerryAnimation.CurrentRow = 2;
                            Jerry.JerryAnimation.CurrentFrame = 0;
                            Score += 100;
                            Task.LeftListReady.Remove(CurrentOval);
                            return;
                        }
                        else if (ListArrowRight.Contains(CurrentOval))
                        {
                            Jerry.Direction = (int)MatrixLocation.DIRECTIONS.RIGHT;
                            Jerry.Step = 0;
                            Jerry.JerryAnimation.CurrentRow = 3;
                            Jerry.JerryAnimation.CurrentFrame = 0;
                            Score += 100;
                            return;
                        }
                        else if (Task.RightListReady.Contains(CurrentOval))
                        {
                            Jerry.Direction = (int)MatrixLocation.DIRECTIONS.RIGHT;
                            Jerry.Step = 0;
                            Jerry.JerryAnimation.CurrentRow = 3;
                            Jerry.JerryAnimation.CurrentFrame = 0;
                            Score += 100;
                            Task.RightListReady.Remove(CurrentOval);
                            return;
                        }
                        else if (ListLoXo.Contains(CurrentOval))
                        {
                            Jerry.JerryAnimation.Jump = true;
                            Score += 100;
                        }
                        else if (Task.WindList.Contains(CurrentOval))
                        {
                            if (!Jerry.JerryAnimation.Jump)
                            {
                                Jerry.Direction = (int)MatrixLocation.DIRECTIONS.FLY;
                            }
                        }
                        else if (Task.HoSauList.Contains(CurrentOval))
                        {
                            if (!Jerry.JerryAnimation.Jump)
                            {
                                Jerry.Direction = (int)MatrixLocation.DIRECTIONS.FALL;
                                Mode = (int)MODE.NONE;
                                Vector2 vt = MatrixLocation.getInstance().mapLocations[(int)CurrentOval.X][(int)CurrentOval.Y];
                                Khoi.Position = vt;
                                Khoi.Position.Y += 350;
                                Khoi.Position.X -= 20;
                                Khoi.Active = true;
                            }
                        }
                        else if (Task.XoayList1.Contains(CurrentOval))
                        {
                            if (Task.XoayList1[0] == CurrentOval)
                            {
                                CurrentOval = Task.XoayList1[1];
                            }
                            else
                            {
                                CurrentOval = Task.XoayList1[0];
                            }
                            Jerry.Position = MatrixLocation.getInstance().mapLocations[(int)CurrentOval.X][(int)CurrentOval.Y];
                            Jerry.Position.X -= 10;
                            Jerry.Position.Y += 610;
                        }
                        else if (ListPhoMat.Contains(CurrentOval))
                        {
                            ListPhoMat.Remove(CurrentOval);
                            Score += 1000;
                        }
                        else if (KeyLocation.Contains(CurrentOval))
                        {
                            KeyLocation.Remove(CurrentOval);
                            DoorStop.DoorAnimation.currentFrame = 1;
                            Score += 200;
                        }
                    }

                    if (CurrentOval == new Vector2(0, 6) ||
                        CurrentOval == new Vector2(1, 7) ||
                        CurrentOval == new Vector2(2, 8) ||
                        CurrentOval == new Vector2(3, 9) ||
                        CurrentOval == new Vector2(4, 10) ||
                        CurrentOval == new Vector2(5, 11) ||
                        CurrentOval == new Vector2(6, 12))
                    {
                        Jerry.Direction = (int)MatrixLocation.DIRECTIONS.DOWN;
                        Jerry.Step = 0;
                        Jerry.JerryAnimation.CurrentRow = 0;
                        Jerry.JerryAnimation.CurrentFrame = 0;
                    }
                }
                else if (Jerry.Direction == (int)MatrixLocation.DIRECTIONS.LEFT)
                {
                    if (Jerry.Step == 0)
                    {
                        double deltaX = MatrixLocation.getInstance().mapLocations[(int)CurrentOval.X][(int)CurrentOval.Y].X - MatrixLocation.getInstance().mapLocations[(int)CurrentOval.X - 1][(int)CurrentOval.Y - 1].X;
                        double deltaY = MatrixLocation.getInstance().mapLocations[(int)CurrentOval.X][(int)CurrentOval.Y].Y - MatrixLocation.getInstance().mapLocations[(int)CurrentOval.X - 1][(int)CurrentOval.Y - 1].Y;
                        LengthStep = (float)deltaY / 4;
                        Alpha = deltaX / deltaY;
                    }
                    float stepLength = (float)Alpha * LengthStep;
                    Jerry.Position.X -= stepLength;
                    Jerry.Position.Y -= LengthStep;
                    Jerry.Step++;
                    if (Jerry.Step >= 4)
                    {
                        CurrentOval.X--;
                        CurrentOval.Y--;
                        Jerry.Step = 0;

                        if (Task.StopLocation == CurrentOval && DoorStop.DoorAnimation.currentFrame == 1 && Jerry.Direction == Task.DirectWin)
                        {
                            GameScreen screen = null;
                            ScreenManager.Level++;
                            if (ScreenManager.Level > ScreenManager.MaxLevel)
                            {
                                screen = new HomeScreen();
                                ScreenManager.Score = 0;
                                ScreenManager.Level = 1;
                            }
                            else
                            {
                                screen = new ScoreScreen();
                                ScreenManager.Score += Score;
                            }
                            ExitScreen();
                            ScreenManager.AddScreen(screen);
                            //ScreenManager.RemoveScreen(this);
                        }
                        else if (ListArrowDown.Contains(CurrentOval))
                        {
                            Jerry.Direction = (int)MatrixLocation.DIRECTIONS.DOWN;
                            Jerry.Step = 0;
                            Jerry.JerryAnimation.CurrentRow = 0;
                            Jerry.JerryAnimation.CurrentFrame = 0;
                            Score += 100;
                            return;
                        }
                        else if (Task.DownListReady.Contains(CurrentOval))
                        {
                            Jerry.Direction = (int)MatrixLocation.DIRECTIONS.DOWN;
                            Jerry.Step = 0;
                            Jerry.JerryAnimation.CurrentRow = 0;
                            Jerry.JerryAnimation.CurrentFrame = 0;
                            Score += 100;
                            Task.DownListReady.Remove(CurrentOval);
                            return;
                        }
                        else if (ListArrowUp.Contains(CurrentOval))
                        {
                            Jerry.Direction = (int)MatrixLocation.DIRECTIONS.UP;
                            Jerry.Step = 0;
                            Jerry.JerryAnimation.CurrentRow = 1;
                            Jerry.JerryAnimation.CurrentFrame = 0;
                            Score += 100;
                            return;
                        }
                        else if (Task.UpListReady.Contains(CurrentOval))
                        {
                            Jerry.Direction = (int)MatrixLocation.DIRECTIONS.UP;
                            Jerry.Step = 0;
                            Jerry.JerryAnimation.CurrentRow = 1;
                            Jerry.JerryAnimation.CurrentFrame = 0;
                            Score += 100;
                            Task.UpListReady.Remove(CurrentOval);
                            return;
                        }
                        else if (Task.LeftListReady.Contains(CurrentOval))
                        {
                            Task.LeftListReady.Remove(CurrentOval);
                        }
                        else if (ListArrowRight.Contains(CurrentOval))
                        {
                            Jerry.Direction = (int)MatrixLocation.DIRECTIONS.RIGHT;
                            Jerry.Step = 0;
                            Jerry.JerryAnimation.CurrentRow = 3;
                            Jerry.JerryAnimation.CurrentFrame = 0;
                            Score += 100;
                            return;
                        }
                        else if (Task.RightListReady.Contains(CurrentOval))
                        {
                            Jerry.Direction = (int)MatrixLocation.DIRECTIONS.RIGHT;
                            Jerry.Step = 0;
                            Jerry.JerryAnimation.CurrentRow = 3;
                            Jerry.JerryAnimation.CurrentFrame = 0;
                            Score += 100;
                            Task.RightListReady.Remove(CurrentOval);
                            return;
                        }
                        else if (ListLoXo.Contains(CurrentOval))
                        {
                            Jerry.JerryAnimation.Jump = true;
                            Score += 200;
                        }
                        else if (Task.WindList.Contains(CurrentOval))
                        {
                            if (!Jerry.JerryAnimation.Jump)
                            {
                                Jerry.Direction = (int)MatrixLocation.DIRECTIONS.FLY;
                            }
                        }
                        else if (Task.HoSauList.Contains(CurrentOval))
                        {
                            if (!Jerry.JerryAnimation.Jump)
                            {
                                Jerry.Direction = (int)MatrixLocation.DIRECTIONS.FALL;
                                Mode = (int)MODE.NONE;
                                Vector2 vt = MatrixLocation.getInstance().mapLocations[(int)CurrentOval.X][(int)CurrentOval.Y];
                                Khoi.Position = vt;
                                Khoi.Active = true;
                                Khoi.Position.Y += 350;
                                Khoi.Position.X -= 20;
                            }
                        }
                        else if (Task.XoayList1.Contains(CurrentOval))
                        {
                            if (Task.XoayList1[0] == CurrentOval)
                            {
                                CurrentOval = Task.XoayList1[1];
                            }
                            else
                            {
                                CurrentOval = Task.XoayList1[0];
                            }
                            Jerry.Position = MatrixLocation.getInstance().mapLocations[(int)CurrentOval.X][(int)CurrentOval.Y];
                            Jerry.Position.X -= 10;
                            Jerry.Position.Y += 610;

                        }
                        else if (ListPhoMat.Contains(CurrentOval))
                        {
                            ListPhoMat.Remove(CurrentOval);
                            Score += 1000;
                        }
                        else if (KeyLocation.Contains(CurrentOval))
                        {
                            KeyLocation.Remove(CurrentOval);
                            DoorStop.DoorAnimation.currentFrame = 1;
                            Score += 200;
                        }
                    }



                    if (CurrentOval == new Vector2(0, 6) ||
                        CurrentOval == new Vector2(1, 5) ||
                        CurrentOval == new Vector2(2, 4) ||
                        CurrentOval == new Vector2(3, 3) ||
                        CurrentOval == new Vector2(4, 2) ||
                        CurrentOval == new Vector2(5, 1) ||
                        CurrentOval == new Vector2(6, 0) )
                    {
                        Jerry.Direction = (int)MatrixLocation.DIRECTIONS.RIGHT;
                        Jerry.Step = 0;
                        Jerry.JerryAnimation.CurrentRow = 3;
                        Jerry.JerryAnimation.CurrentFrame = 0;
                    }
                }
                else if (Jerry.Direction == (int)MatrixLocation.DIRECTIONS.RIGHT)
                {
                    if (Jerry.Step == 0)
                    {
                        double deltaX = MatrixLocation.getInstance().mapLocations[(int)CurrentOval.X + 1][(int)CurrentOval.Y + 1].X - MatrixLocation.getInstance().mapLocations[(int)CurrentOval.X][(int)CurrentOval.Y].X;
                        double deltaY = MatrixLocation.getInstance().mapLocations[(int)CurrentOval.X + 1][(int)CurrentOval.Y + 1].Y - MatrixLocation.getInstance().mapLocations[(int)CurrentOval.X][(int)CurrentOval.Y].Y;
                        LengthStep = (float)deltaY / 4;
                        Alpha = deltaX / deltaY;
                    }
                    float stepLength = (float)Alpha * LengthStep;
                    Jerry.Position.X += stepLength;
                    Jerry.Position.Y += LengthStep;
                    Jerry.Step++;
                    if (Jerry.Step >= 4)
                    {
                        CurrentOval.X++;
                        CurrentOval.Y++;
                        Jerry.Step = 0;

                        if (Task.StopLocation == CurrentOval && DoorStop.DoorAnimation.currentFrame == 1 && Jerry.Direction == Task.DirectWin)
                        {
                            GameScreen screen = null;
                            ScreenManager.Level++;
                            if (ScreenManager.Level > ScreenManager.MaxLevel)
                            {
                                screen = new HomeScreen();
                                ScreenManager.Score = 0;
                                ScreenManager.Level = 1;
                            }
                            else
                            {
                                screen = new ScoreScreen();
                                ScreenManager.Score += Score;
                            }
                            ScreenManager.AddScreen(screen);
                            ScreenManager.RemoveScreen(this);
                        }
                        else if (ListArrowDown.Contains(CurrentOval))
                        {
                            Jerry.Direction = (int)MatrixLocation.DIRECTIONS.DOWN;
                            Jerry.Step = 0;
                            Jerry.JerryAnimation.CurrentRow = 0;
                            Jerry.JerryAnimation.CurrentFrame = 0;
                            Score += 100;
                            return;
                        }
                        else if (Task.DownListReady.Contains(CurrentOval))
                        {
                            Jerry.Direction = (int)MatrixLocation.DIRECTIONS.DOWN;
                            Jerry.Step = 0;
                            Jerry.JerryAnimation.CurrentRow = 0;
                            Jerry.JerryAnimation.CurrentFrame = 0;
                            Score += 100;
                            Task.DownListReady.Remove(CurrentOval);
                            return;
                        }
                        else if (ListArrowUp.Contains(CurrentOval))
                        {
                            Jerry.Direction = (int)MatrixLocation.DIRECTIONS.UP;
                            Jerry.Step = 0;
                            Jerry.JerryAnimation.CurrentRow = 1;
                            Jerry.JerryAnimation.CurrentFrame = 0;
                            Score += 100;
                            return;
                        }
                        else if (Task.UpListReady.Contains(CurrentOval))
                        {
                            Jerry.Direction = (int)MatrixLocation.DIRECTIONS.UP;
                            Jerry.Step = 0;
                            Jerry.JerryAnimation.CurrentRow = 1;
                            Jerry.JerryAnimation.CurrentFrame = 0;
                            Score += 100;
                            Task.UpListReady.Remove(CurrentOval);
                            return;
                        }
                        else if (ListArrowLeft.Contains(CurrentOval))
                        {
                            Jerry.Direction = (int)MatrixLocation.DIRECTIONS.LEFT;
                            Jerry.Step = 0;
                            Jerry.JerryAnimation.CurrentRow = 2;
                            Jerry.JerryAnimation.CurrentFrame = 0;
                            Score += 100;
                            return;
                        }
                        else if (Task.LeftListReady.Contains(CurrentOval))
                        {
                            Jerry.Direction = (int)MatrixLocation.DIRECTIONS.LEFT;
                            Jerry.Step = 0;
                            Jerry.JerryAnimation.CurrentRow = 2;
                            Jerry.JerryAnimation.CurrentFrame = 0;
                            Score += 100;
                            Task.LeftListReady.Remove(CurrentOval);
                            return;
                        }
                        else if (Task.RightListReady.Contains(CurrentOval))
                        {
                            Task.RightListReady.Remove(CurrentOval);
                        }
                        else if (ListLoXo.Contains(CurrentOval))
                        {
                            Jerry.JerryAnimation.Jump = true;
                            Score += 200;
                        }
                        else if (Task.WindList.Contains(CurrentOval))
                        {
                            if (!Jerry.JerryAnimation.Jump)
                            {
                                Jerry.Direction = (int)MatrixLocation.DIRECTIONS.FLY;
                            }
                        }
                        else if (Task.HoSauList.Contains(CurrentOval))
                        {
                            if (!Jerry.JerryAnimation.Jump)
                            {
                                Jerry.Direction = (int)MatrixLocation.DIRECTIONS.FALL;
                                Mode = (int)MODE.NONE;
                                Vector2 vt = MatrixLocation.getInstance().mapLocations[(int)CurrentOval.X][(int)CurrentOval.Y];
                                Khoi.Position = vt;
                                Khoi.Position.Y += 350;
                                Khoi.Active = true;
                                Khoi.Position.X -= 20;
                            }
                        }
                        else if (Task.XoayList1.Contains(CurrentOval))
                        {
                            if (Task.XoayList1[0] == CurrentOval)
                            {
                                CurrentOval = Task.XoayList1[1];
                            }
                            else
                            {
                                CurrentOval = Task.XoayList1[0];
                            }
                            Jerry.Position = MatrixLocation.getInstance().mapLocations[(int)CurrentOval.X][(int)CurrentOval.Y];
                            Jerry.Position.X -= 10;
                            Jerry.Position.Y += 610;

                        }
                        else if (ListPhoMat.Contains(CurrentOval))
                        {
                            ListPhoMat.Remove(CurrentOval);
                            Score += 1000;
                        }
                        else if (KeyLocation.Contains(CurrentOval))
                        {
                            KeyLocation.Remove(CurrentOval);
                            DoorStop.DoorAnimation.currentFrame = 1;
                            Score += 200;
                        }
                    }

                    if (CurrentOval == new Vector2(6, 12) ||
                        CurrentOval == new Vector2(7, 11) ||
                        CurrentOval == new Vector2(8, 10) ||
                        CurrentOval == new Vector2(9, 9) ||
                        CurrentOval == new Vector2(10, 8) ||
                        CurrentOval == new Vector2(11, 7) ||
                        CurrentOval == new Vector2(12, 6))
                    {
                        Jerry.Direction = (int)MatrixLocation.DIRECTIONS.LEFT;
                        Jerry.Step = 0;
                        Jerry.JerryAnimation.CurrentRow = 2;
                        Jerry.JerryAnimation.CurrentFrame = 0;
                    }
                }
                else if (Jerry.Direction == (int)MatrixLocation.DIRECTIONS.FLY)
                {
                    Jerry.Position.X += 30;
                    if (Jerry.Position.X > 500)
                    {
                        Reset();
                        return;
                    }
                }
                eslapTime = 0;
            }
            Jerry.Update(gameTime);
        }

        /// <summary>
        /// Draws the menu.
        /// </summary>
        public override void Draw(GameTime gameTime)
        {
            ScreenManager.GraphicsDevice.SetRenderTarget(ScreenManager.FinalImageTarget);
            ScreenManager.GraphicsDevice.Clear(Color.CornflowerBlue);

            // we will do all of our drawing here
            ScreenManager.GraphicsDevice.SetRenderTarget(null);

            SpriteBatch.Begin();
            SpriteBatch.Draw(BackgroundTexture, new Vector2(480, 0), null, Color.White, MathHelper.PiOver2, Vector2.Zero, Vector2.One, SpriteEffects.None, 0f);
            SpriteBatch.Draw(PanelScoreTexture, new Vector2(480, 0), null, Color.White, MathHelper.PiOver2, Vector2.Zero, Vector2.One, SpriteEffects.None, 0f);
            SpriteBatch.Draw(KhayTexture, KhayPosition, null, Color.White, MathHelper.PiOver2, Vector2.Zero, Vector2.One, SpriteEffects.None, 0f);
            SpriteBatch.Draw(LoXoBigTexture, LoXoBigPosition, null, Color.White, MathHelper.PiOver2, Vector2.Zero, Vector2.One, SpriteEffects.None, 0f);
            controller.Draw(SpriteBatch);

            DoorStart.Draw(SpriteBatch);
            DoorStop.Draw(SpriteBatch);

            for (int i = 0; i < Lifes.Length; i++)
            {
                Lifes[i].Draw(SpriteBatch);
            }

            foreach (Vector2 vector in Task.KeyList)
            {
                Vector2 vt = MatrixLocation.getInstance().mapLocations[(int)vector.X][(int)vector.Y];
                vt.X += 25;
                vt.Y -= 30;
                SpriteBatch.Draw(KeyTexture, vt, null, Color.White, MathHelper.PiOver2, Vector2.Zero, Vector2.One, SpriteEffects.None, 0f);
            }

            foreach (Vector2 vector in Task.HoSauList)
            {
                Vector2 v = MatrixLocation.getInstance().mapLocations[(int)vector.X][(int)vector.Y];
                v.X += 25;
                v.Y -= 55;
                SpriteBatch.Draw(HoSauTexture, v, null, Color.White, MathHelper.PiOver2, Vector2.Zero, Vector2.One, SpriteEffects.None, 0f);
            }

            foreach (Vector2 vector in Task.UpListReady)
            {
                Vector2 v = MatrixLocation.getInstance().mapLocations[(int)vector.X][(int)vector.Y];
                v.X += 18;
                v.Y -= 25;
                SpriteBatch.Draw(ArrowUpOranTexture, v, null, Color.White, MathHelper.PiOver2, Vector2.Zero, Vector2.One, SpriteEffects.None, 0f);
            }

            foreach (Vector2 vector in Task.DownListReady)
            {
                Vector2 v = MatrixLocation.getInstance().mapLocations[(int)vector.X][(int)vector.Y];
                v.X += 18;
                v.Y -= 25;
                SpriteBatch.Draw(ArrowDownOranTexture, v, null, Color.White, MathHelper.PiOver2, Vector2.Zero, Vector2.One, SpriteEffects.None, 0f);
            }

            foreach (Vector2 vector in Task.LeftListReady)
            {
                Vector2 v = MatrixLocation.getInstance().mapLocations[(int)vector.X][(int)vector.Y];
                v.X += 18;
                v.Y -= 25;
                SpriteBatch.Draw(ArrowLeftOranTexture, v, null, Color.White, MathHelper.PiOver2, Vector2.Zero, Vector2.One, SpriteEffects.None, 0f);
            }

            foreach (Vector2 vector in Task.RightListReady)
            {
                Vector2 v = MatrixLocation.getInstance().mapLocations[(int)vector.X][(int)vector.Y];
                v.X += 18;
                v.Y -= 25;
                SpriteBatch.Draw(ArrowRightOranTexture, v, null, Color.White, MathHelper.PiOver2, Vector2.Zero, Vector2.One, SpriteEffects.None, 0f);
            }

            foreach (Vector2 vector in Task.XoayList1)
            {
                Vector2 v = MatrixLocation.getInstance().mapLocations[(int)vector.X][(int)vector.Y];
                v.X += 30;
                v.Y -= 60;
                SpriteBatch.Draw(XoayTexture, v, null, Color.White, MathHelper.PiOver2, Vector2.Zero, Vector2.One, SpriteEffects.None, 0f);
            }

            foreach (WindSprite win in ListWind )
            {
                /*Vector2 v = MatrixLocation.getInstance().mapLocations[(int)vector.X][(int)vector.Y];
                v.X += 40;
                v.Y -= 40;
                SpriteBatch.Draw(WindTexture, v, null, Color.White, MathHelper.PiOver2, Vector2.Zero, Vector2.One, SpriteEffects.None, 0f);*/
                win.Draw(SpriteBatch);
            }

            SpriteBatch.DrawString(font, ""+ Score, new Vector2(450, 50), Color.Cyan, MathHelper.PiOver2, Vector2.Zero, 1f, SpriteEffects.None, 0f);
            SpriteBatch.DrawString(font, "" + ScreenManager.Level, new Vector2(390, 50), Color.DarkBlue, MathHelper.PiOver2, Vector2.Zero, 1f, SpriteEffects.None, 0f);

            SpriteBatch.DrawString(font, "" + (ExistUp - ListArrowUp.Count ), new Vector2(100, 40), Color.White, MathHelper.PiOver2, Vector2.Zero, 1.2f, SpriteEffects.None, 0f);
            SpriteBatch.DrawString(font, "" + (ExistRight - ListArrowRight.Count), new Vector2(100, 90), Color.White, MathHelper.PiOver2, Vector2.Zero, 1.2f, SpriteEffects.None, 0f);
            SpriteBatch.DrawString(font, "" + (ExistLeft - ListArrowLeft.Count), new Vector2(50, 25), Color.White, MathHelper.PiOver2, Vector2.Zero, 1.2f, SpriteEffects.None, 0f);
            SpriteBatch.DrawString(font, "" + (ExistDown - ListArrowDown.Count), new Vector2(50, 110), Color.White, MathHelper.PiOver2, Vector2.Zero, 1.2f, SpriteEffects.None, 0f);
            SpriteBatch.DrawString(font, "" + (ExistLoXo - ListLoXo.Count), new Vector2(40, 190), Color.White, MathHelper.PiOver2, Vector2.Zero, 1.2f, SpriteEffects.None, 0f);

            foreach (Vector2 vector in ListPhoMat)
            {
                Vector2 v = MatrixLocation.getInstance().mapLocations[(int)vector.X][(int)vector.Y];
                v.X += 35;
                v.Y -= 30;
                SpriteBatch.Draw(PhoMatTexture, v, null, Color.White, MathHelper.PiOver2, Vector2.Zero, Vector2.One, SpriteEffects.None, 0f);
            }

            foreach (Vector2 vector in ListArrowDown )
            {
                Vector2 v = MatrixLocation.getInstance().mapLocations[(int)vector.X][(int)vector.Y];
                v.X += 25;
                v.Y -= 35;
                SpriteBatch.Draw(ArrowDownTexture, v, null, Color.White, MathHelper.PiOver2, Vector2.Zero, Vector2.One, SpriteEffects.None, 0f);
            }

            foreach (Vector2 vector in ListArrowRight)
            {
                Vector2 v = MatrixLocation.getInstance().mapLocations[(int)vector.X][(int)vector.Y];
                v.X += 25;
                v.Y -= 35;
                SpriteBatch.Draw(ArrowRightTexture, v, null, Color.White, MathHelper.PiOver2, Vector2.Zero, Vector2.One, SpriteEffects.None, 0f);
            }

            foreach (Vector2 vector in ListArrowUp)
            {
                Vector2 v = MatrixLocation.getInstance().mapLocations[(int)vector.X][(int)vector.Y];
                v.X += 25;
                v.Y -= 35;
                SpriteBatch.Draw(ArrowUpTexture, v, null, Color.White, MathHelper.PiOver2, Vector2.Zero, Vector2.One, SpriteEffects.None, 0f);
            }

            foreach (Vector2 vector in ListArrowLeft)
            {
                Vector2 v = MatrixLocation.getInstance().mapLocations[(int)vector.X][(int)vector.Y];
                v.X += 25;
                v.Y -= 35;
                SpriteBatch.Draw(ArrowLeftTexture, v, null, Color.White, MathHelper.PiOver2, Vector2.Zero, Vector2.One, SpriteEffects.None, 0f);
            }

            foreach (Vector2 vector in ListLoXo)
            {
                Vector2 v = MatrixLocation.getInstance().mapLocations[(int)vector.X][(int)vector.Y];
                v.X += 25;
                v.Y -= 35;
                SpriteBatch.Draw(LoXoSmallTexture, v, null, Color.White, MathHelper.PiOver2, Vector2.Zero, Vector2.One, SpriteEffects.None, 0f);
            }

            if(Jerry.Direction == (int)MatrixLocation.DIRECTIONS.FALL)
            {
                Khoi.Draw(SpriteBatch);
            }
            else
            {
                Jerry.Draw(SpriteBatch);
            }
            
            if (CurrentArrow != Vector2.Zero && CurrentTexturwArrow != null)
            {
                Vector2 position = CurrentArrow;
                position.X += 30;
                position.Y -= 20;
                SpriteBatch.Draw(CurrentTexturwArrow, position, null, Color.White, MathHelper.PiOver2, Vector2.Zero, Vector2.One, SpriteEffects.None, 0f);
            }

            SpriteBatch.End();
        }
    }
}
